package com.imooc.bilibili.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.imooc.bilibili.dao.UserMomentsDao;
import com.imooc.bilibili.domain.UserMoment;
import com.imooc.bilibili.rabbit.RabbitSendMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ProjectName imooc-bilibili
 *
 * @author xieyucan
 * <br>CreateDate 2022/10/24 10:41
 */
@Service
public class UserMomentsService {
    @Resource
    private UserMomentsDao userMomentsDao;

    @Resource
    private RabbitSendMessage rabbitSendMessage;

    @Resource
    private RedisTemplate redisTemplate;

    public void saveMoments(UserMoment userMoment) {
        userMoment.setCreateTime(new Date());
        userMomentsDao.saveMoments(userMoment);
        rabbitSendMessage.sendMessage(userMoment);
    }

    public List<UserMoment> getUserSubscribedUserMoment(Long userId)
    {
        String key="subscribed"+userId;
        String subscribed=(String) redisTemplate.opsForValue().get(key);
        List<UserMoment> list = JSONArray.parseArray(subscribed, UserMoment.class);
        return list;
    }

}
